<script setup lang="ts">
import type { Directive } from 'vue';
// 默认图片地址
import defaultImgSrc from './assets/logo.svg'
// 批量导入本地图片
const modulesArr = import.meta.glob('./assets/angular*.jpg',{eager:true});
 const imgArr = Object.values(modulesArr).map((item:any)=> item.default)
// console.log('modulesArr', imgArr);
// 自定义图片懒加载指令
 const vLazy:Directive<HTMLImageElement,string>= (el, binding)=>{
   const observer = new IntersectionObserver(entry=>{
    // 默认图片地址
    el.src = defaultImgSrc;
    if(entry[0].intersectionRatio > 0 && entry[0].isIntersecting) {
      // 延迟替换地址为了看效果
      setTimeout(()=>{
         el.src = binding.value;
      },800)
      observer.unobserve(el);
    }
   })
   observer.observe(el);
 }
</script>

<template>

  <main>
    <el-button>我是按钮</el-button>
    <div class="box">
      <img v-for="item in imgArr"  width="200" height="200" v-lazy="item"/>
    </div>
  </main>
</template>

<style scoped>
.box {
  width:200px;
  height:400px;
  border: 1px solid green;
  overflow-y: scroll;
}
header {
  line-height: 1.5;
}

.logo {
  display: block;
  margin: 0 auto 2rem;
}

@media (min-width: 1024px) {
  header {
    display: flex;
    place-items: center;
    padding-right: calc(var(--section-gap) / 2);
  }

  .logo {
    margin: 0 2rem 0 0;
  }

  header .wrapper {
    display: flex;
    place-items: flex-start;
    flex-wrap: wrap;
  }
}
</style>
